package com.education.welco.service.impl;

import com.education.common.pojo.AjaxResult;
import com.education.common.utils.Convert;
import com.education.welco.mapper.SAnsweringCommentMapper;
import com.education.welco.mapper.SAnsweringLikeMapper;
import com.education.welco.mapper.SReportMapper;
import com.education.welco.pojo.SAnsweringComment;
import com.education.welco.pojo.SCommentResq;
import com.education.welco.pojo.vo.MessageList;
import com.education.welco.service.ISAnsweringCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * (答疑讨论) 服务层实现
 *
 * @author d
 * @date 2022-06-20
 */
@RestController
public class SAnsweringCommentServiceImpl implements ISAnsweringCommentService {

    @Autowired
    private SAnsweringLikeMapper sAnsweringLikeMapper;
    @Autowired
    private SAnsweringCommentMapper sAnsweringCommentMapper;
    @Autowired
    private SReportMapper sReportMapper;

    @Override
    public List<MessageList> selectCommentListBySAnswerId(List<Integer> twoIntList) {
        return sAnsweringCommentMapper.selectCommentListBySAnswerId(twoIntList);
    }

    @Override
    public int postComment(String userId, String name, String image, String resId, String text, String level) {
        SAnsweringComment sAnsweringComment = new SAnsweringComment();
        sAnsweringComment.setCPostId(Integer.valueOf(resId));
        sAnsweringComment.setCUserId(userId);
        sAnsweringComment.setCUserName(name);
        sAnsweringComment.setCUserIco(image);
        sAnsweringComment.setCCreateBy(name);
        sAnsweringComment.setCPlLevel(Integer.valueOf(level));
        sAnsweringComment.setCPlText(text);
        sAnsweringComment.setNLikeNum(0);
        sAnsweringComment.setNDiscussNum(0);
        sAnsweringComment.setTCreateTime(new Date());

        int i = sAnsweringCommentMapper.insertSysUserforumsComment(sAnsweringComment);

        return i;
    }

    @Override
    public AjaxResult selectCommentListById(String answerId, String userId, String type) {
        //查出达人圈发布内容的一级评论列表  参数依次为 1.发布内容id 2.排序方式 3.评论层级
        List<SCommentResq> commentList = sAnsweringCommentMapper.selectSysUserForumsCommentListOther(answerId, type, "1");
        //填充发布时间
        for (SCommentResq sCommentResq : commentList) {
            sCommentResq.setCommentDateStr(getTimeDIFF(sCommentResq.getCommentDate()));
        }
        //拿到一级评论的主键  利用map将评论对象转化为评论主键
        List<String> commentIds = commentList.stream().map(SCommentResq::getCommentId).collect(Collectors.toList());
        //当前评论列表的二级评论列表
        List<SCommentResq> secondLevelList = new ArrayList<>();
        //一级评论的主键list不为空
        if (commentIds.size() > 0) {
            secondLevelList = sAnsweringCommentMapper.selectSecondLevelForumsCommentList(commentIds, type, "2");
            //填充发布时间
            for (SCommentResq sCommentResq : secondLevelList) {
                sCommentResq.setCommentDateStr(getTimeDIFF(sCommentResq.getCommentDate()));
            }
        }
        //回调点赞 查询当前用户的点赞评论id列表    目的是给查询出的评论做标记(当前用户点赞过哪条评论)
        List<String> likeItIds = sAnsweringLikeMapper.selectForumsIdsByUserIdAndType(userId, "5");
        //设置点赞状态
        setLikeStatus(commentList, likeItIds);//1级
        setLikeStatus(secondLevelList, likeItIds);//2级

        //回调举报状态 查询当前用户的举报评论id列表
        HashSet<String> reportIds = sReportMapper.selectForumsReportIds(userId, "5");
        //设置举报状态
        setReportStatus(commentList, reportIds);//1级
        setReportStatus(secondLevelList, reportIds);//2级

        //一二级评论进行组合
        for (SCommentResq sysForumsCommentResq : commentList) {
            //拿到一级评论id作为pid设置二级评论列表
            String commentId = sysForumsCommentResq.getCommentId();
            List<SCommentResq> children = secondLevelList.stream().filter(forumsCommentResq -> commentId.equals(forumsCommentResq.getPostId().toString()))
                    .collect(Collectors.toList());
            sysForumsCommentResq.setList(children);
            sysForumsCommentResq.setCommentNum(children.size() + "");
        }
        return AjaxResult.success(commentList);
    }

    //31分钟前
    public String getTimeDIFF(Date time_ago) {
        Date time_now = new Date();
        long diff = 0;
        String CountTime = "";
        int year = 0, month = 0, day = 0;
        long hours = 0, minutes = 0, s = 0;

        diff = time_now.getTime() - time_ago.getTime();
        Calendar currentTimes = dataToCalendar(time_now);//当前系统时间转Calendar类型
        Calendar pastTimes = dataToCalendar(time_ago);//查询的数据时间转Calendar类型
        year = currentTimes.get(Calendar.YEAR) - pastTimes.get(Calendar.YEAR);//获取年
        month = currentTimes.get(Calendar.MONTH) - pastTimes.get(Calendar.MONTH);
        day = currentTimes.get(Calendar.DAY_OF_MONTH) - pastTimes.get(Calendar.DAY_OF_MONTH);
        if (month < 0) {
            month = (month + 12) % 12;//获取月
            year--;
        }
        if (day < 0) {
            month -= 1;
            currentTimes.add(Calendar.MONTH, -1); //减一个月
            day = day + currentTimes.getActualMaximum(Calendar.DAY_OF_MONTH);//获取日
        }
        long days = diff / (1000 * 60 * 60 * 24);
        hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60); //获取时
        minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);  //获取分钟
        s = (diff / 1000 - days * 24 * 60 * 60 - hours * 60 * 60 - minutes * 60);//获取秒
        CountTime = "" + year + "年" + month + "月" + day + "天" + hours + "小时" + minutes + "分" + s + "秒";

        //System.out.println(CountTime);
        String dd = year != 0 ? year + "年前" :
                month != 0 ? month + "月前" :
                        day != 0 ? day + "天前" :
                                hours != 0 ? hours + "小时前" :
                                        minutes != 0 ? minutes + "分钟前" :
                                                s + "秒前";
        //System.out.println(dd);
        return dd;
    }

    //Date类型转Calendar类型
    public static Calendar dataToCalendar(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.setTime(date);
        return calendar;
    }

    @Override
    public AjaxResult selectCommentListById2(String answerId, String userId, String type) {
        //查出达人圈发布内容的一级评论列表  参数依次为 1.发布内容id 2.排序方式 3.评论层级
        List<SCommentResq> commentList = sAnsweringCommentMapper.selectSysUserForumsCommentListOther(answerId, type, "1");
        //拿到一级评论的主键  利用map将评论对象转化为评论主键
        List<String> commentIds = commentList.stream().map(SCommentResq::getCommentId).collect(Collectors.toList());

        //当前评论列表的二级评论列表
        List<SCommentResq> secondLevelList = new ArrayList<>();
        //一级评论的主键list不为空
        if (commentIds.size() > 0) {
            secondLevelList = sAnsweringCommentMapper.selectSecondLevelForumsCommentList(commentIds, type, "2");
        }

        /*//回调点赞 查询当前用户的点赞评论id列表    目的是给查询出的评论做标记(当前用户点赞过哪条评论)
        List<String> likeItIds = sAnsweringLikeMapper.selectForumsIdsByUserIdAndType(userId, "5");
        //设置点赞状态
        setLikeStatus(commentList, likeItIds);//1级
        setLikeStatus(secondLevelList, likeItIds);//2级

        //回调举报状态 查询当前用户的举报评论id列表
        HashSet<String> reportIds = sReportMapper.selectForumsReportIds(userId, "5");
        //设置举报状态
        setReportStatus(commentList, reportIds);//1级
        setReportStatus(secondLevelList, reportIds);//2级*/

        //一二级评论进行   组合
        for (SCommentResq sysForumsCommentResq : commentList) {
            //拿到一级评论id作为pid设置二级评论列表
            String commentId = sysForumsCommentResq.getCommentId();

            List<SCommentResq> children = secondLevelList.stream().filter(forumsCommentResq -> commentId.equals(forumsCommentResq.getPostId().toString())).collect(Collectors.toList());
            sysForumsCommentResq.setList(children);

            sysForumsCommentResq.setCommentNum(children.size() + "");
        }

        return AjaxResult.success(commentList);
    }

    /**
     * 设置点赞状态
     */
    private void setLikeStatus(List<SCommentResq> list, List<String> likeItIds) {
        //判断是否点赞    设置子级评论列表
        for (SCommentResq sysForumsCommentResq : list) {
            String commentId = sysForumsCommentResq.getCommentId();
            if (likeItIds.contains(commentId)) {
                //已经点赞
                sysForumsCommentResq.setIsLike("1");
            } else {
                //没有点赞
                sysForumsCommentResq.setIsLike("2");
            }
        }
    }

    /**
     * 设置举报状态
     */
    private void setReportStatus(List<SCommentResq> list, HashSet<String> reportItIds) {
        //判断是否点赞    设置子级评论列表
        for (SCommentResq sysForumsCommentResq : list) {
            String commentId = sysForumsCommentResq.getCommentId();
            if (reportItIds.contains(commentId)) {
                //已经举报
                sysForumsCommentResq.setIsReport("1");
            } else {
                //没有举报
                sysForumsCommentResq.setIsReport("2");
            }
        }
    }

    /**
     * 查询(答疑讨论)信息
     *
     * @param cPlId (答疑讨论)ID
     * @return (答疑讨论)信息
     */
    @Override
    public SAnsweringComment selectSAnsweringCommentById(Integer cPlId) {
        return sAnsweringCommentMapper.selectSAnsweringCommentById(cPlId);
    }

    /**
     * 查询(答疑讨论)列表
     *
     * @param sAnsweringComment (答疑讨论)信息
     * @return (答疑讨论)集合
     */
    @Override
    public List<SAnsweringComment> selectSAnsweringCommentList(SAnsweringComment sAnsweringComment) {
        return sAnsweringCommentMapper.selectSAnsweringCommentList(sAnsweringComment);
    }

    /**
     * 新增(答疑讨论)
     *
     * @param sAnsweringComment (答疑讨论)信息
     * @return 结果
     */
    @Override
    public int insertSAnsweringComment(SAnsweringComment sAnsweringComment) {
        sAnsweringComment.setCCreateBy(sAnsweringComment.getCUserName());
        return sAnsweringCommentMapper.insertSAnsweringComment(sAnsweringComment);
    }

    /**
     * 修改(答疑讨论)
     *
     * @param sAnsweringComment (答疑讨论)信息
     * @return 结果
     */
    @Override
    public int updateSAnsweringComment(SAnsweringComment sAnsweringComment) {
        return sAnsweringCommentMapper.updateSAnsweringComment(sAnsweringComment);
    }

    /**
     * 删除(答疑讨论)对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteSAnsweringCommentByIds(String ids) {
        return sAnsweringCommentMapper.deleteSAnsweringCommentByIds(Convert.toStrArray(ids));
    }

    @Override
    public int stickSAnsweringComment(Integer cPostId, Integer cPlId) {
        return sAnsweringCommentMapper.stickSAnsweringComment(cPostId, cPlId);
    }

    @Override
    public SAnsweringComment selectCommentListByResId(Integer resId) {
        return sAnsweringCommentMapper.selectCommentListByResId(resId);
    }

    //搜索每个答疑的讨论人数量
    @Override
    public List<Integer> countEachAnswerTalkPeopleNum(Integer id) {
        return sAnsweringCommentMapper.countEachAnswerTalkPeopleNum(id);
    }

    //搜索每个答疑的讨论数量
    @Override
    public Integer countEachAnswerTalkNum(Integer id) {
        return sAnsweringCommentMapper.countEachAnswerTalkNum(id);
    }

    //置顶|取消置顶(答疑讨论)
    @Override
    public Integer stickSAnsweringCommentForTeacher(Integer cPlId, Integer stick) {
        return sAnsweringCommentMapper.stickSAnsweringCommentForTeacher(cPlId, stick);
    }
}